/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tubes1sister.tracker;

import java.util.LinkedList;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Muhammad Anis
 */
public class TrackerResponse {

    //parameter yang dibutuhkan untuk mengirim response ke client

    public String failure_reason = "";
    public String warning_message = "";
    public int interval;
    public int complete;
    public int incomplete;
    public LinkedList<String> peers = new LinkedList<String>();

    //constructor
    public TrackerResponse() {
        //do nothing
        this.interval = 3000;
    }

    public TrackerResponse(String failure_reaseon, String warning_message, int interval, int complete, int incomplete, LinkedList<String> peers) {
        this.failure_reason = failure_reaseon;
        this.warning_message = warning_message;
        this.interval = interval;
        this.complete = complete;
        this.incomplete = incomplete;
        this.peers = peers;
    }

    /*public void getResponseFromFile(String filename){
    String value="";
    try{
    File file = new File(filename);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    doc.getDocumentElement().normalize();
    //System.out.println("Root Element: " + doc.getDocumentElement().getNodeName());
    NodeList nodeLst = doc.getElementsByTagName("peers");
    //System.out.println("Information of all peers: ");

    for(int s=0;s<nodeLst.getLength();s++){
    Node fstNode = nodeLst.item(s);
    if(fstNode.getNodeType() == Node.ELEMENT_NODE){
    Element fstElement = (Element)fstNode;
    NodeList fstNmElmntLst = fstElement.getElementsByTagName("ip");
    Element fstIpElement = (Element)fstNmElmntLst.item(0);
    NodeList fstIP = fstIpElement.getChildNodes();
    // System.out.println("IP : " + ((Node)fstIP.item(0)).getNodeValue());
    //masukkan ke daftar peers
    value+=((Node)fstIP.item(0)).getNodeValue(); //-->IP
    NodeList portElementLst = fstElement.getElementsByTagName("ports");
    Element portElement = (Element)portElementLst.item(0);
    NodeList port = portElement.getChildNodes();
    //System.out.println("Port: " + ((Node)port.item(0)).getNodeValue());
    value+=":"+((Node)port.item(0)).getNodeValue(); //-->ports
    //masukkan ke peers
    peers.add(value);
    value="";
    }
    }
    }catch(Exception e){
    e.printStackTrace();
    }
    }*/
    public void getResponseFromFile(String fileyangdicari) {
        Connection conn = null;
        String value="";
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost/sister", "root", "anis123");
            java.sql.Statement stmt = null;
            ResultSet rs = null;
            stmt = conn.createStatement();
            String sqlQuery = "SELECT * FROM tubes WHERE filename='" + fileyangdicari + "'";
            rs = stmt.executeQuery(sqlQuery);
            while (rs.next()) {
                //System.out.println(rs.getString("nama") + " : " + rs.getString("judul"));
                value=rs.getString("ip")+":"+rs.getString("port");
                peers.add(value);
                value="";
            }
        } catch (Exception e) {
        }
    }
   
    public String makeResponse() throws Exception {
        String temp = "";
        String[] temp2;
        temp += "Failure_Reason " + this.failure_reason
                + "@" + "warning_message " + this.warning_message
                + "@" + "interval " + this.interval
                + "@" + "complete " + this.complete
                + "@" + "incomplete " + this.incomplete
                + "@" + "peers ";
        for (int i = 0; i < peers.size(); i++) {
            temp2 = peers.get(i).split(":");
            if (temp2.length < 2) {
                throw new Exception("Format Peers Salah");
            }
            temp += temp2[0] + ":" + temp2[1] + " ";
        }
        return temp;
    }
}
